const path = require('path');
const { log } = require('console');
const HtmlPlugin = require('@rspack/plugin-html')

// 判断是否生产环境
const isProduction = log('mode: ', process.env.NODE_ENV) || process.env.NODE_ENV === 'production';

module.exports = {
    mode: isProduction ? 'production' : 'development',
    entry: { fiexible: './src/js/flexible.js', main: './src/index.ts' },
    output: {
        path: path.resolve(__dirname, './calculator'),
        clean: true,
        filename: 'js/[name].js',
        chunkFilename: "js/[name].chunk.js", // 主入口文件分割出来的文件命名
        assetModuleFilename: "media/[name].[hash][ext]", // 图片、字体等资源模块命名方式
    },
    devtool: isProduction ? false : 'cheap-module-source-map',
    builtins: {
        presetEnv: {
            targets: [
                "last 1 version",
                "> 1%",
                "not dead"],
        },
        target: ['web'],
    },
    plugins: [
        new HtmlPlugin({
            inject: 'body',
            template: path.resolve(__dirname, './public/index.html'),
            favicon: path.resolve(__dirname, './public/greater.svg')
        })
    ],
    resolve: {
        extensions: ['.js', '.ts'],
        alias: {
            '@base': path.resolve(__dirname, './src/base')
        }
    },
    module: {
        rules: [
            {
                test: /.css$/,
                type: 'css'
            },
            {
                test: /.svg$/,
                type: "asset/resource"
            }
        ]
    },
    optimization: {
        splitChunks: {
            chunks: 'all',
            filename: 'js/[name].chunk.js'
        }
    },
}